package backend;


import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class ProductByID {
	private ProductDetails product;
	private String url="http://eiffel.itba.edu.ar/hci/service/Catalog.groovy?method=GetProduct&product_id=23";

	public void initialize() {
		try{
			Connect con = new Connect();
			Document dom = con.getConection(url);
			NodeList prods = dom.getElementsByTagName("product");
			Node node = null;
			NodeList att = null;
			String prodID=null, catID=null, subCatID=null, rank=null, price=null, name=null, url=null;
			String actors=null, format=null, language=null, subtitles=null, region=null;
			String ratio=null, discs=null, release=null, time=null, asin=null;
			for(int i=0; i<prods.getLength(); i++){
				node = prods.item(i);
				att = node.getChildNodes();
								
				prodID=node.getAttributes().item(0).getNodeValue();
				for(int j=0; j<att.getLength(); j++){
					if(att.item(j).getNodeName().equals("category_id"))
						catID=att.item(j).getTextContent();
					else if(att.item(j).getNodeName().equals("subcategory_id"))
						subCatID=att.item(j).getTextContent();
					else if(att.item(j).getNodeName().equals("name"))
						name=att.item(j).getTextContent();
					else if(att.item(j).getNodeName().equals("sales_rank"))
						rank=att.item(j).getTextContent();
					else if(att.item(j).getNodeName().equals("price"))
						price=att.item(j).getTextContent();
					else if(att.item(j).getNodeName().equals("image_url"))
						url=att.item(j).getTextContent();
					else if(att.item(j).getNodeName().equals("actors"))
						actors=att.item(j).getTextContent();
					else if(att.item(j).getNodeName().equals("format"))
						format=att.item(j).getTextContent();
					else if(att.item(j).getNodeName().equals("language"))
						language=att.item(j).getTextContent();
					else if(att.item(j).getNodeName().equals("subtitles"))
						subtitles=att.item(j).getTextContent();
					else if(att.item(j).getNodeName().equals("region"))
						region=att.item(j).getTextContent();
					else if(att.item(j).getNodeName().equals("aspect_ratio"))
						ratio=att.item(j).getTextContent();
					else if(att.item(j).getNodeName().equals("number_discs"))
						discs=att.item(j).getTextContent();
					else if(att.item(j).getNodeName().equals("release_date"))
						release=att.item(j).getTextContent();
					else if(att.item(j).getNodeName().equals("run_time"))
						time=att.item(j).getTextContent();
					else if(att.item(j).getNodeName().equals("ASIN"))
						asin=att.item(j).getTextContent();
				}
				
				this.product = new ProductDetails(prodID, catID, subCatID, name, rank, price, url, 
						actors, format, language, subtitles, region, ratio, 
						discs, release, time, asin);
					
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	public ProductDetails getProduct(){
		return this.product;
	}
	
}
